let n = 2;
let isHappy = function (n) {
  const seen = new Set();
  while (n !== 1 && !seen.has(n)) {
    seen.add(n)
    // console.log('seen:', seen)
    let sum = 0;
    while (n > 0) {
      let digit = n % 10; // 取出当前数字的最后一位
      sum += digit * digit;
      n = Math.floor(n / 10); // 去掉最后一位数字
    }
    n = sum
  }

  if(n == 1){
    return true
  }else{
    return false
  }
};

console.log(isHappy(n))
